﻿Imports System.Xml
Imports System.Xml.Serialization
Imports System.Drawing.Imaging

<XmlRootAttribute("Tile")>
Public Class MapTile
	<XmlAttribute("ID")> Public TileID As String = ""
	<XmlAttribute("Solid")> Public Solid As Boolean = False
	<XmlAttribute("Rotation")> Public Rotation As Integer = 0

	<XmlIgnore> Public Const Width As Integer = 32
	<XmlIgnore> Public Const Height As Integer = 32

	<XmlIgnore> Public Color As Color = Drawing.Color.White
	<XmlIgnore> Public Graphics As Graphics
	<XmlIgnore> Public BitMap As Bitmap
	<XmlIgnore> Public BitMapOriginal As Bitmap


	Public Sub New()
		Solid = False
		BitMap = New Bitmap(Width, Height)
		Graphics = Drawing.Graphics.FromImage(BitMap)
		SetSolidColor(Drawing.Color.White)
	End Sub

	Public Sub New(bitmap As Bitmap, tileid As String, solid As Boolean)
		Me.BitMap = bitmap
		Me.BitMapOriginal = New Bitmap(bitmap)
		Me.TileID = tileid
		Me.Solid = solid
		Graphics = Drawing.Graphics.FromImage(bitmap)
	End Sub

	Public Sub FinishLoad()

	End Sub

	Public Sub SetSolidColor(color As Color)
		Me.Color = color
		Graphics.FillRectangle(New SolidBrush(color), 0, 0, Width, Height)
	End Sub

	Public Sub CopyFrom(maptile As MapTile)
		If maptile IsNot Nothing Then
			TileID = maptile.TileID
			BitMapOriginal = maptile.BitMap.Clone
			Graphics.DrawImage(maptile.BitMap, 0, 0, Width, Height)

			Solid = maptile.Solid
			Color = maptile.Color
		End If
	End Sub

	Public Sub Rotate()

		Graphics.DrawImage(New Bitmap(BitMapOriginal), 0, 0, Width, Height)
		If Rotation = 0 Then
			BitMap.RotateFlip(RotateFlipType.RotateNoneFlipNone)
		ElseIf Rotation = 90 Then
			BitMap.RotateFlip(RotateFlipType.Rotate90FlipNone)
		ElseIf Rotation = 180 Then
			BitMap.RotateFlip(RotateFlipType.Rotate180FlipNone)
		ElseIf Rotation = 270 Then
			BitMap.RotateFlip(RotateFlipType.Rotate270FlipNone)
		End If

	End Sub

End Class
